<?php
class Tarefas {
    private  $db;
	public function Tarefas() {
	    $this->db=Options::getDb();
	}
	
	
	public function checkStatus($lido,$concluido){
		$arr=array();
		if((int)$concluido == 1){
			$arr["status"]= "Concluida";
			$arr["cor"]="success";
		}
		else if($lido == 1 && $concluido == 0 ){
			$arr["status"]= "Foi vista e não foi concluida";
			$arr["cor"]="warning";
		}
		else{
			$arr["status"]="Aberta";
			$arr["cor"]="danger";
		}
		return $arr;
	}
	
	public function selectTarefasfaNaoFeitas($concluida=0,$categoria=false){
		try {
			$and="";
			if($categoria != false){
				$and = " AND c.id = ".$categoria;
			}
			$Conexao = Conexao::Conect();
			$sql="SELECT t.id,
						t.titulo,
						t.data,
						t.lida,
						t.concluida,
						c.nome as categoria
					FROM {$this->db}.tarefa t
					INNER JOIN relatorio.categoria c ON(c.id = t.categoria)      
					WHERE
					  concluida =".$concluida."
					  	$and	
					ORDER BY data DESC
					LIMIT 0,500;";
			$data = $Conexao->query ( $sql );
			if ( $data->rowCount() > 0) {
				return $data->fetchAll();
			}
			else{
				return  false;
			}
		} catch ( PDOException $e ) {
			echo 'ERROR: ' . $e->getMessage ();
			return  false;
		}
	}
	
	public function selectAllCategoria(){
		try {
			$Conexao = Conexao::Conect();
			$sql="SELECT
						c.nome,
						c.id
					FROM 
						 {$this->db}.categoria c
					WHERE
						1
					ORDER BY nome ASC;";
			$data = $Conexao->query ( $sql );
			if ( $data->rowCount() > 0) {
				return $data->fetchAll();
			}
			else{
				return  false;
			}
		} catch ( PDOException $e ) {
			echo 'ERROR: ' . $e->getMessage ();
			return  false;
		}
	}
	public function serch($q){
		try {
			$q= strip_tags($q);
			$q= trim($q);
			$Conexao = Conexao::Conect();
			$sql="SELECT id,
						titulo,
						data,
						lida,
						concluida
					FROM {$this->db}.tarefa
					WHERE
						 `titulo` LIKE '%".$q."%'
					ORDER BY data DESC;";
			$data = $Conexao->query ( $sql );

			if ( $data->rowCount() > 0) {
				return $data->fetchAll();
			}
			else{
				return  false;
			}
		} catch ( PDOException $e ) {
			echo 'ERROR: ' . $e->getMessage ();
			return  false;
		}
	}
	
	public function temTarefaNaoFeitas(){
		try {
			$Conexao = Conexao::Conect();
			$sql="SELECT id
					FROM {$this->db}.tarefa
					WHERE
					  concluida =0;";
			$data = $Conexao->query ( $sql );
			if ( $data->rowCount() > 0) {
				return true;
			}
			else{
				return  false;
			}
		} catch ( PDOException $e ) {
			echo 'ERROR: ' . $e->getMessage ();
			return  false;
		}
	}
	
	public function statusTarefa($id){
		try {
			$Conexao = Conexao::Conect();
			$sql="SELECT concluida
					FROM {$this->db}.tarefa
					WHERE
					  id =".$id.";";
			$data = $Conexao->query ( $sql );
			if ( $data->rowCount() > 0) {
				$row = $data->fetch();
				return $row["concluida"];
			}
			else{
				return  0;
			}
		} catch ( PDOException $e ) {
			echo 'ERROR: ' . $e->getMessage ();
			return  0;
		}
	}
	
	
	
	public function novaTarefa($titulo) {
		try {
			$Conexao = Conexao::Conect ();
			$sql = "INSERT INTO `{$this->db}`.`tarefa` (
					`id` ,
					`titulo` ,
					`data` ,
					`lida` ,
					`concluida` 
					)
					VALUES (
						NULL , 
						'" . $titulo . "', 
						NOW(),
						0,
						0
					);";
			$data = $Conexao->query ( $sql );
			return $data->rowCount (); // 1
		} catch ( PDOException $e ) {
			echo 'ERROR: ' . $e->getMessage ();
			return false;
		}
	}
	
	public function marcaTarefaLida($tarefas) {
		$separa = explode ( ",", $tarefas );
		$Usuario = unserialize($_SESSION["usuario"]);
		$idUsuario = $Usuario->getId();
		if (is_array ( $separa )) {
			foreach ( $separa as $tarefa ) {
				$this->insereTarefaUsuarioLida($tarefa, $idUsuario);
				$this->updateLida($tarefa);
			}
		}
	}
	
	public function insereTarefaUsuarioLida($tarefa,$usuario) {
		try {
			$Conexao = Conexao::Conect ();
			$sql = "INSERT INTO {$this->db}.tarefas_lidas (
					`id` ,
					`tarefa` ,
					`usuario` ,
					`data` 
					)
					VALUES (
						NULL ,
						'" . $tarefa . "',
						'" . $usuario . "',
						NOW()		
					);";
			$data = $Conexao->query ( $sql );
		} catch ( PDOException $e ) {
			echo $e->getMessage ();
		}
	}
	
	
	public function insereTarefaUsuarioFeita($tarefa,$usuario) {
		try {
			$Conexao = Conexao::Conect ();
			$sql = "INSERT INTO {$this->db}.tarefas_feitas (
					`id` ,
					`tarefa` ,
					`usuario` ,
					`data`
					)
					VALUES (
						NULL ,
						'" . $tarefa . "',
						'" . $usuario . "',
						NOW()
					);";
			$data = $Conexao->query ( $sql );
		} catch ( PDOException $e ) {
			echo $e->getMessage ();
		}
	}
	public function removeTarefaUsuarioFeita($tarefa,$usuario) {
		try {
			$Conexao = Conexao::Conect ();
			$sql = "DELETE FROM `{$this->db}`.`tarefas_feitas` 
						WHERE 
							`tarefas_feitas`.`tarefa` = $tarefa 
							AND  `tarefas_feitas`.`usuario` = $usuario";
			$data = $Conexao->query ( $sql );
		} catch ( PDOException $e ) {
			echo $e->getMessage ();
		}
	}
	
	
	
	public function updateLida($tarefa) {
		try {
			$Conexao = Conexao::Conect ();
			$stmt = $Conexao->prepare('UPDATE `{$this->db}`.`tarefa` SET lida = 1 WHERE id = :id');
			$stmt->execute(array(
					':id'   => $tarefa
			));
			} catch ( PDOException $e ) {
				echo $e->getMessage ();
		}	
	}
	
	public function updateTarefaConcluida($tarefa,$status){
		try {
			$Conexao = Conexao::Conect ();
			$stmt = $Conexao->prepare('UPDATE `{$this->db}`.`tarefa` SET concluida = '.$status.' WHERE id = :id');
			$stmt->execute(array(
					':id'   => $tarefa
			));
			return true;
		} catch ( PDOException $e ) {
			echo $e->getMessage ();
			return false;
		}
	}
}